package com.xckj.teacher.presenter;

import androidx.annotation.NonNull;

import com.xckj.teacher.bean.BaseBean;
import com.xckj.teacher.bean.UpdateBean;
import com.xckj.teacher.framework.net.RetrofitManager;
import com.xckj.teacher.global.Constant;
import com.xckj.teacher.iview.IUpdateView;

import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author: ChenYao
 * @Describe: 更新
 * @Date: 2024/6/17 23:45
 */
public class UpdatePresenter {
    private final IUpdateView updateView;

    public UpdatePresenter(IUpdateView updateView) {
        this.updateView = updateView;
    }

    /**
     * 更新
     *
     * @param versionName 当前版本
     */
    public void update(String versionName) {
        updateView.showLoading();
        Call<BaseBean<UpdateBean>> call = RetrofitManager.getInstance().getService().update("andriod", versionName);
        call.enqueue(new Callback<BaseBean<UpdateBean>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<UpdateBean>> call, @NonNull Response<BaseBean<UpdateBean>> response) {
                if (response.isSuccessful()) {
                    BaseBean<UpdateBean> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        updateView.success(baseBean.data);
                    } else {
                        updateView.failed(baseBean.msg);
                    }
                } else {
                    updateView.failed("请求失败");
                }
                updateView.hideLoading();
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<UpdateBean>> call, @NonNull Throwable t) {
                t.printStackTrace();
                updateView.failed("请求失败");
                updateView.hideLoading();
            }
        });
    }

    /**
     * 版本模式
     *
     * @param versionName 当前版本
     */
    public void versionMode(String versionName) {
        Call<BaseBean<UpdateBean>> call = RetrofitManager.getInstance().getService().versionMode("andriod", versionName);
        call.enqueue(new Callback<BaseBean<UpdateBean>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<UpdateBean>> call, @NonNull Response<BaseBean<UpdateBean>> response) {
                if (response.isSuccessful()) {
                    BaseBean<UpdateBean> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        updateView.versionModeSuccess(baseBean.data);
                    } else {
                        updateView.failed(baseBean.msg);
                    }
                } else {
                    updateView.failed("请求失败");
                }
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<UpdateBean>> call, @NonNull Throwable t) {
                t.printStackTrace();
                updateView.failed("请求失败");
            }
        });
    }
}
